﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using BenevolentSunData;

namespace BenevolentSun
{
    public static class ActionMap
    {

        public static Vector2 CameraMovement()
        {
            Vector2 cameraVeloc = new Vector2();
            cameraVeloc = InputManager.Thumbsticks(PlayerIndex.One).Left;
            if (InputManager.IsKeyPressed(Keys.Up))
                cameraVeloc.Y = 1;
            if (InputManager.IsKeyPressed(Keys.Down))
                cameraVeloc.Y = -1;
            if (InputManager.IsKeyPressed(Keys.Right))
                cameraVeloc.X = 1;
            if (InputManager.IsKeyPressed(Keys.Left))
                cameraVeloc.X = -1;
            return cameraVeloc;
        }

        public static Point SelectorMovement()
        {
            Point coords = new Point();

            if (MoveSelectorUp())
                coords.Y = -1;
            if (MoveSelectorDown())
                coords.Y = 1;
            if (MoveSelectorLeft())
                coords.X = -1;
            if (MoveSelectorRight())
                coords.X = 1;

            // if its a new button press it is always yes
            // if it is not new, then there should be a delay

            return coords;
        }

        private static TimeSpan repeatTimer = new TimeSpan();

        public static void Update(GameTime gameTime)
        {
            repeatTimer += gameTime.ElapsedGameTime;
            if (repeatTimer.TotalMilliseconds > 150)
                repeatTimer = TimeSpan.Zero;
        }

        public static bool MoveSelectorDown()
        {
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadDown) 
                || InputManager.IsNewKeyPress(Keys.Down)
                //|| InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.LeftThumbstickDown)
                )
            { repeatTimer = new TimeSpan(1); return true; }

            if ((InputManager.IsButtonPress(PlayerIndex.One, Buttons.DPadDown) 
                //|| InputManager.Thumbsticks(PlayerIndex.One).Left.Y < -0.5
                )
                && repeatTimer.Ticks == 0)
                return true;
            return false;
        }

        public static bool MoveSelectorUp()
        {
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadUp) || InputManager.IsNewKeyPress(Keys.Up)
                //|| InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.LeftThumbstickUp)
                )
            { repeatTimer = new TimeSpan(1); return true; }
            if ((InputManager.IsButtonPress(PlayerIndex.One, Buttons.DPadUp) 
                //|| InputManager.Thumbsticks(PlayerIndex.One).Left.Y > 0.5
                )
                && repeatTimer.Ticks == 0)
                return true;
            return false;
        }

        public static bool MoveSelectorLeft()
        {
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadLeft) || InputManager.IsNewKeyPress(Keys.Left)
                //|| InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.LeftThumbstickLeft)
                )
            { repeatTimer = new TimeSpan(1); return true; }
            if ((InputManager.IsButtonPress(PlayerIndex.One, Buttons.DPadLeft) 
                //|| InputManager.Thumbsticks(PlayerIndex.One).Left.X < -0.5
                )
                && repeatTimer.Ticks == 0)
                return true;
            return false;
        }

        public static bool MoveSelectorRight()
        {
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadRight) || InputManager.IsNewKeyPress(Keys.Right)
                //|| InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.LeftThumbstickRight)
                )
            { repeatTimer = new TimeSpan(1); return true; }
            if ((InputManager.IsButtonPress(PlayerIndex.One, Buttons.DPadRight) 
                //|| InputManager.Thumbsticks(PlayerIndex.One).Left.X > 0.5
                )
                && repeatTimer.Ticks == 0)
                return true;
            return false;
        }

        public static bool CellInteraction()
        {
            return InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.A) || InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.RightTrigger) || InputManager.IsNewKeyPress(Keys.Enter);
        }

        public static bool Pause()
        {
            return InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.Start) || InputManager.IsNewKeyPress(Keys.Escape);
        }




        // Gui Navigation
        public static bool Skip
        {
            get 
            {
                return InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.Start) || InputManager.IsNewKeyPress(Keys.Space);
            }
        }

        public static bool Start
        {
            get
            {
                return InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.Start) || InputManager.IsNewKeyPress(Keys.Space);
            }
        }

        public static bool Left
        {
            get
            {
                if (
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadLeft) ||
                    InputManager.IsNewKeyPress(Keys.Left)
                    )
                {
                    repeatTimer = new TimeSpan(1);
                    return true;
                }

                // repeatable
                if (
                    (
                        InputManager.IsButtonPress(PlayerIndex.One, Buttons.DPadLeft) ||
                        InputManager.IsKeyPressed(Keys.Left) ||
                        InputManager.Thumbsticks(PlayerIndex.One).Left.X < -0.5
                    ) &&
                    repeatTimer.Ticks == 0)
                {
                    return true;
                }

                return false;
            }
        }

        public static bool Right
        {
            get
            {
                if (
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadRight) || 
                    InputManager.IsNewKeyPress(Keys.Right)
                    )
                { 
                    repeatTimer = new TimeSpan(1);
                    return true; 
                }

                // repeatable
                if (
                    (
                        InputManager.IsButtonPress(PlayerIndex.One, Buttons.DPadRight) ||
                        InputManager.IsKeyPressed(Keys.Right) ||
                        InputManager.Thumbsticks(PlayerIndex.One).Left.X > 0.5
                    ) &&
                    repeatTimer.Ticks == 0)
                {
                    return true;
                }

                return false;
            }
        }

        public static bool Confirm
        {
            get
            {
                return InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.A) || InputManager.IsNewKeyPress(Keys.Enter);
            }
        }

        public static bool Cancel
        {
            get
            {
                return InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.B) || InputManager.IsNewKeyPress(Keys.Escape);
            }
        }

        public static bool AnyButton
        {
            get
            {
                return 
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.A) ||
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.B) ||
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.Y) ||
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.X) ||
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.LeftShoulder) ||
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.RightShoulder) ||
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.Start) ||
                    InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.Back) ||
                    InputManager.IsNewKeyPress(Keys.Enter) ||
                    InputManager.IsNewKeyPress(Keys.Space) || 
                    InputManager.IsNewKeyPress(Keys.Escape);
            }
        }

    }

}
